<?php
/**

*/
class DptSignupController extends App_Controller_Action{

	public function preDispatch()
	{
		$user = $this->getUserId();
		if (!empty($user)) {
			// If the user is logged in, can't really signup again without logging out
				$request = $this->getRequest();
				$from_dpt = $request->getParam('from_dpt');
				$client_id=$user;
			
				$model_client = new Model_UserClient();
				$serviceInfo=$model_client->getClientService($client_id,$from_dpt);
				
				if (empty($serviceInfo)){
					$clientInfo=$model_client->getClient($client_id);
					$login_account=$clientInfo['email'];
					
					$dpt_login_arr=array();
					$dpt_login_arr['dpt_account_login'] = $login_account;
					$dpt_login_arr['login_area'] = 'member';
					$dpt_login_arr['email'] = $clientInfo['email'];
					$dpt_login_arr['password'] = $clientInfo['password'];
					$dpt_login_arr['admin_user_id'] = '';
					$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
					
					$model_site_service = Model_ServiceFactory::Create($from_dpt);
	            	$dpt_account_id = $model_site_service->addRemoteUser($client_id);	//$dpt_account_id:remote user_id		
					
					$model_client->linkClientService($client_id, $from_dpt, $dpt_account_id, $dpt_login_arr);
					$this->_redirect('/client/index/index/success_msg/Service has been subsribed Successfully!');
					
				}else{
					$this->_redirect('/client');
				}
		}
	}

	public function indexAction()
	{
		$request = $this->getRequest();
		$from_dpt = $request->getParam('from_dpt');
		
		$form = new Form_Signup();
		$this->view->form = $form;
		$this->view->signup_success=false;

		if ($this->getRequest()->isPost()) {

			//check IP
			$ip=Model_ActionLog::getIp();
			if (1==0){
				$this->view->error_msg = 'IP address not allowed.';				
			}else{
			
				$formData = $this->getRequest()->getPost();
	
				if ($form->isValid($formData)) {
	
					$email = $formData['email'];
					$password = $formData['password'];
					//timezone = $formData['timezone'];
	
					$model_client = new Model_UserClient();
					$client_id = $model_client->signupClient($email, $password);
					
					if ($client_id!=-1){
						
		    			$body = "Dear YesUp User,<br><br>
									Thank you for signing up an account with us!<br><br>
									Please activate your account by clicking on the following link:<br><br>
									<a href='https://account.yesup.com/dpt-signup/activate/id/".$client_id."/from_dpt/".$from_dpt."' target=_blank>https://account.yesup.com/dpt-signup/activate/id/".$client_id."</a><br><br>
									
									YesUp Account Username: ".$email." <br><br>
									
									Please feel free to contact us if you have any questions.<br><br>
									
									Best regards,<br><br>
									
									YesUp Account Support Team<br>
										    			";
					    $mail = new Zend_Mail('UTF-8');
					    $mail->setBodyHtml($body)
					         ->setFrom('no-reply@yesup.com', 'YesUp Account')
					         ->addTo($email, '')
					         ->setSubject("Please activate your YesUp Account")
					         ->send();    			
	    			
	    			
						//$this->_redirect("/default/login/client/msg/signup");	
						$this->_redirect("/dpt-signup/confirm");	
						
					}else{
						$this->_redirect("/login/client/msg/"."Sorry, email already exists in system or data error.");					
					}
					
				} else {
	
					$form->populate($formData);
				}
			}
		}
		else{
/*
			$timezone = App_Timezone::getDefaultTimeZone();
			$form->populate(array('timezone'=>$timezone));
*/
		}

	}

	public function confirmAction()
	{
	}


	public function activateAction()
	{
		$request = $this->getRequest();
    	$client_id = $request->getParam('id'); 	
    	$from_dpt = $request->getParam('from_dpt'); 	
    	if (!empty($client_id)){

			$model_client = new Model_UserClient();
    		$clientInfo=$model_client->getClient($client_id);
    		if ($clientInfo){
				$this->_redirect("/login/client/msg/"."Your account is already activated, please login.");
				exit;					    			
    		}
    		
			$result = $model_client->activateClient($client_id);
			if ($result){    		
				$clientInfo=$model_client->getClient($client_id);
				$login_account=$clientInfo['email'];
				
				$dpt_login_arr=array();
				$dpt_login_arr['dpt_account_login'] = $login_account;
				$dpt_login_arr['login_area'] = 'member';
				$dpt_login_arr['email'] = $clientInfo['email'];
				$dpt_login_arr['password'] = $clientInfo['password'];
				$dpt_login_arr['admin_user_id'] = '';
				$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
				
				$model_site_service = Model_ServiceFactory::Create($from_dpt);
            	$dpt_account_id = $model_site_service->addRemoteUser($client_id);	//$dpt_account_id:remote user_id		
				
				$model_client->linkClientService($client_id, $from_dpt, $dpt_account_id, $dpt_login_arr);
            					
				$session = $this->getSession();
				$storage = new App_Auth_Storage_Session($session,'client');
				//Zend_Debug::dump($storage->read());		
				$storage->write(array(
                    'user_id'=>$client_id,
                    'user_name'=>$login_account,
                    'user_role'=>'client'));
				
				$this->_redirect("/client/index/index/success_msg/Service has been subsribed Successfully!");
			}else	    		
				$this->view->error_msg="Sorry, account activation failed.";					
    	}
		
	}
	

}